package com.xiaomili.musicservice;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    public static final int ORDER_PLAY = 0x00;
    public static final int ORDER_PAUSE = 0x01;
    public static final int ORDER_STOP = 0x02;
    public static final String ORDER_KEY = "control";
    public static final int ORDER_CUSTOM_PROGRESS = 0x03;
    public static final int ORDER_UNKNOW = 0x99;
    public static final String SET_PROGRESS = "setProgress";
    private SeekBar seekBar;
    private Intent intent;
    private ServiceConnection conn;
    private MyMusicService.MyBinder myBinder;
    private int mProgress;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == ORDER_CUSTOM_PROGRESS) {
                int[] data = myBinder.getMusicTime();
                seekBar.setMax(data[0]);
                seekBar.setProgress(data[1]);
                handler.sendEmptyMessageDelayed(ORDER_CUSTOM_PROGRESS, 1000);
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekBar = (SeekBar) findViewById(R.id.seekBar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.i(TAG, "fromUser-->" + fromUser);
//                if (fromUser) {
//
//                    handler.sendEmptyMessage(ORDER_CUSTOM_PROGRESS);
//                }

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mProgress = seekBar.getProgress();
                intent.putExtra(SET_PROGRESS, mProgress);
                startService(intent);
            }
        });
        intent = new Intent(this, MyMusicService.class);
        startService(intent);
        conn = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                myBinder = (MyMusicService.MyBinder) service;
                int[] data = myBinder.getMusicTime();
                seekBar.setMax(data[0]);
                seekBar.setProgress(data[1]);
                if (myBinder.isMusicPlaying()) {
                    handler.sendEmptyMessage(ORDER_CUSTOM_PROGRESS);
                }

            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        };
        bindService(intent, conn, BIND_AUTO_CREATE);
    }

    public void btnStart(View view) {
        intent.putExtra(ORDER_KEY, ORDER_PLAY);
        startService(intent);
        handler.sendEmptyMessage(ORDER_CUSTOM_PROGRESS);
    }

    public void btnPause(View view) {
        intent.putExtra(ORDER_KEY, ORDER_PAUSE);
        startService(intent);
        handler.removeMessages(ORDER_CUSTOM_PROGRESS);
    }

    public void btnStop(View view) {
        intent.putExtra(ORDER_KEY, ORDER_STOP);
        startService(intent);
        seekBar.setProgress(0);
        handler.removeMessages(ORDER_CUSTOM_PROGRESS);
    }
}
